package com.remiss.seckilldemo.controller;

import com.remiss.seckilldemo.pojo.User;
import com.remiss.seckilldemo.service.IGoodsService;
import com.remiss.seckilldemo.service.IUserService;
import com.remiss.seckilldemo.vo.DetailVO;
import com.remiss.seckilldemo.vo.GoodsVO;
import com.remiss.seckilldemo.vo.ResposeBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.crypto.Data;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/goods")
public class GoodsCotroller {
    @Autowired
    private IUserService iUserService;
    @Autowired
    private IGoodsService iGoodsService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ThymeleafViewResolver thymeleafViewResolver;

    /**
     *
     * @param model
     * @param user
     * @return
     */
    @RequestMapping(value = "/toList" , produces = "text/html;charset =utf-8")
    @ResponseBody
    public String toList(Model model, User user,HttpServletRequest request,HttpServletResponse response){
//        从redistribution获取页面
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String html = (String) valueOperations.get("goodsList");
        if (!StringUtils.isEmpty(html)){
            return html;
        }
        model.addAttribute("user",user);
        model.addAttribute("goodsList",iGoodsService.findGoodsVO());
//        如果为空则进行手动渲染，并存入redis中
        WebContext webContext = new WebContext(request,response,request.getServletContext(),
                request.getLocale(),model.asMap());
        html = thymeleafViewResolver.getTemplateEngine().process("goodsList", webContext);
        if(!StringUtils.isEmpty(html)){
            valueOperations.set("goodsList",html,5, TimeUnit.MINUTES);
        }
        return html;
    }




    @RequestMapping( "/detail/{goodsId}")
    @ResponseBody
    public ResposeBean toDetail(Model model, User user, @PathVariable Long goodsId){

        GoodsVO goodsVO = iGoodsService.findGoodsVOByGoodsId(goodsId);
        Date startDate = goodsVO.getStartDate();
        Date endDate = goodsVO.getEndDate();
        Date now = new Date();
//        秒杀状态
        int seckillStatus = 0;
        int remainSeconds = 0;
        if(now.before(startDate)){
            remainSeconds = ((int) ((startDate.getTime() - now.getTime()) / 1000));
        }else if (now.before(endDate)){
            seckillStatus = 1;
        }else {
            seckillStatus = 2;
            remainSeconds = -1;
        }
        DetailVO detailVO = new DetailVO();
        detailVO.setUser(user);
        detailVO.setGoodsVO(goodsVO);
        detailVO.setSecKillStatus(seckillStatus);
        detailVO.setRemainSeconds(remainSeconds);
        return ResposeBean.success(detailVO);
//        return "goodsDetail";
    }
}
